/*
 * Created on Jun 10, 2010
 */
package ro.letsdoitromania.forms;

import javax.microedition.lcdui.*;
import com.mgmaps.app.APPCommands;
import com.mgmaps.tools.UITools;
import com.mgmaps.utils.LS;
import ro.letsdoitromania.Commands;
import ro.letsdoitromania.LDIRCanvas;
import ro.letsdoitromania.LDIRMidlet;
import ro.letsdoitromania.Settings;

public class MapTypeForm implements CommandListener {
  private Form form;
  
  private ChoiceGroup cgMapType;
  
  private static MapTypeForm instance;
  private String language;
  
  public static MapTypeForm getInstance() {
    if (instance == null)
      instance = new MapTypeForm();
    return instance;
  }
  
  private MapTypeForm() {
    language = Settings.language;
  }
  
  private void resetForm() {
    cgMapType = null;
  }

  public void show() {
    if (language != Settings.language)
      resetForm();
    
    if (cgMapType == null)
      cgMapType = new ChoiceGroup(LS.get("ldir.maptype.itemtitle"), ChoiceGroup.EXCLUSIVE,
          Settings.MAP_NAMES, null);
    
    form = UITools.buildForm(form, LS.get("ldir.maptype.title"), new Item[] { cgMapType },
        new Command[] { Commands.cinstance.saveCommand, APPCommands.instance.backCommand }, this);
    
    cgMapType.setSelectedIndex(LDIRCanvas.instance.getMapType(), true);
    
    Display.getDisplay(LDIRMidlet.instance).setCurrent(form);
  }
  
  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == Commands.cinstance.saveCommand) {
      int mt = cgMapType.getSelectedIndex();
      LDIRCanvas.instance.setMapType(mt);
      Settings.save();
    }
    
    Display.getDisplay(LDIRMidlet.instance).setCurrent(LDIRCanvas.instance);
  }
}
